package com.edu.bhu.admin.controller.admin;

import java.util.List;

import com.edu.bhu.common.config.OverallConfig;
import com.edu.bhu.common.core.domain.entity.SysUser;
import com.edu.bhu.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.admin.domain.LibBook;
import com.edu.bhu.admin.service.ILibBookService;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import com.edu.bhu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图书Controller
 * 
 * @author wwb
 * @date 2022-02-11
 */
@Controller
@RequestMapping("/admin/book")
public class LibBookController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(LibBookController.class);

    private String prefix = "admin/book";

    @Autowired
    private ILibBookService libBookService;

    @RequiresPermissions("admin:book:view")
    @GetMapping()
    public String book()
    {
        return prefix + "/book";
    }

    /**
     * 借阅信息回收页面
     */
    @RequiresPermissions("admin:book:view")
    @GetMapping("/bookRecycle")
    public String loanRecycle()
    {
        return prefix + "/bookRecycle";
    }

    /**
     * 查询图书列表
     */
    @RequiresPermissions("admin:book:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LibBook libBook)
    {
        startPage();
        List<LibBook> list = libBookService.selectLibBookList(libBook);
        return getDataTable(list);
    }

    /**
     * 查询图书列表(关联图书类型)
     */
    @RequiresPermissions("admin:book:list")
    @PostMapping("/listWithType")
    @ResponseBody
    public TableDataInfo listWithType(LibBook libBook)
    {
        startPage();
        List<LibBook> list = libBookService.selectLibBookWithTypeList(libBook);
        return getDataTable(list);
    }

    /**
     * 查询图书列表(关联图书类型)(被删除的)
     */
    @RequiresPermissions("admin:book:list")
    @PostMapping("/listWithTypeDel")
    @ResponseBody
    public TableDataInfo listWithTypeDel(LibBook libBook)
    {
        startPage();
        List<LibBook> list = libBookService.selectLibBookWithTypeListDel(libBook);
        return getDataTable(list);
    }

    /**
     * 导出图书列表
     */
    @RequiresPermissions("admin:book:export")
    @Log(title = "图书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LibBook libBook)
    {
        List<LibBook> list = libBookService.selectLibBookList(libBook);
        ExcelUtil<LibBook> util = new ExcelUtil<LibBook>(LibBook.class);
        return util.exportExcel(list, "图书数据");
    }

    /**
     * 新增图书
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存图书
     */
    @RequiresPermissions("admin:book:add")
    @Log(title = "图书", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LibBook libBook,@RequestParam("picture") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(OverallConfig.getAvatarPath(), file);
                libBook.setImages(avatar);
            }
            if (libBookService.insertLibBook(libBook) > 0)
            {
                return success();
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("更新菜品失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 修改图书
     */
    @RequiresPermissions("admin:book:edit")
    @GetMapping("/edit/{bookId}")
    public String edit(@PathVariable("bookId") Long bookId, ModelMap mmap)
    {
        LibBook libBook = libBookService.selectLibBookByBookId(bookId);
        mmap.put("libBook", libBook);
        return prefix + "/edit";
    }

    /**
     * 修改保存图书
     */
    @RequiresPermissions("admin:book:edit")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LibBook libBook,@RequestParam("picture") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(OverallConfig.getAvatarPath(), file);
                libBook.setImages(avatar);
            }
            if (libBookService.updateLibBook(libBook) > 0)
            {
                return success();
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("更新菜品失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 图书状态修改
     */
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @RequiresPermissions("admin:book:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(LibBook libBook)
    {
        return toAjax(libBookService.changeStatus(libBook));
    }

    /**
     * 删除图书
     */
    @RequiresPermissions("admin:book:remove")
    @Log(title = "图书", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(libBookService.deleteLibBookByBookIds(ids));
    }

    /**
     * 删除图书(永久)
     */
    @RequiresPermissions("admin:book:remove")
    @Log(title = "图书", businessType = BusinessType.DELETE)
    @PostMapping( "/removeForever")
    @ResponseBody
    public AjaxResult removeForever(String ids)
    {
        return toAjax(libBookService.deleteLibBookByBookIdsForever(ids));
    }


    /**
     * 校验bookNo是否唯一
     */
    @PostMapping("/checkBookNo")
    @ResponseBody
    public String checkEmailUnique(LibBook libBook)
    {
        return libBookService.checkBookNoUnique(libBook);
    }

    /**
     * 批量还原删除数据 wangweibo
     */
    @RequiresPermissions("admin:book:edit")
    @Log(title = "图书信息", businessType = BusinessType.UPDATE)
    @PostMapping( "/batchRestore")
    @ResponseBody
    public AjaxResult batchRestore(String ids)
    {
        return toAjax(libBookService.updateLibBookBatch(ids));
    }
}
